package org.vy.dal;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Properties;

public class UploadData {
	private static Properties prop = null;
	
	private static String m_package = "org/vy/dal/dao";
	
	static{
		prop = new Properties();
		FileInputStream fis = null;
		try{
	        fis = new FileInputStream("D:\\VYSofts\\VyManagement\\src\\org\\vy\\dal\\db.properties");
	        prop.load(fis);
		}catch(Exception e){
			e.printStackTrace();
			if(fis != null){
				try{
					fis.close();
				}catch(Exception ex){
					//DO Nothing
					ex.printStackTrace();
				}
			}
		}
    }
	
	public static Connection getConnection() throws SQLException, ClassNotFoundException {
		Class.forName(prop.getProperty("db.driver"));
		Connection dbConn = null;

		if(prop != null)
			dbConn = DriverManager.getConnection(prop.getProperty("db.url"), 
					prop.getProperty("db.user"),
					prop.getProperty("db.password"));

		return dbConn;
	}
	
	public static void main(String[] s1) throws Exception {
		try{
			Connection con = DALConnector.getConnection();
			PreparedStatement stmt = con.prepareStatement("INSERT INTO TEMP_CITY VALUES(?,?)");
			BufferedReader br = new BufferedReader(new FileReader("D:\\VYSofts\\Database\\MoreCity.csv"));
			if(con != null){
				try{
					String line = br.readLine();
					String[] parts = null;
					while(line != null){
						parts = line.split(",");
						stmt.setString(1, parts[1]);
						stmt.setString(2, parts[0]);
						stmt.execute();
						line = br.readLine();
					}
				}catch(Exception ex){
					ex.printStackTrace();
				}
			}
		}catch(Exception e){
			System.out.println("Connection Failure :: "+ e.getMessage());
			e.printStackTrace();
		}
	}
}
